﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Agenda.Helpers;
using Agenda.Forms.Common;
using System.Data.OleDb;
using Agenda.Service;
using Agenda.Entities;

namespace Agenda.Forms
{
    public partial class Agregar : BaseForm
    {
        public List<Control> myControls = new List<Control>();

        public Agregar()
        {
            InitializeComponent();
        }

        private void Agregar_Load(object sender, EventArgs e)
        {
            label1.Text = "Nombre";
            label2.Text = "Apellido";
            label3.Text = "Celular 1";
            label4.Text = "Celular 2";
            label5.Text = "Telefono";
            label6.Text = "Obra";
            label7.Text = "Información Extra";
            label10.Text = "E-mail";

            myControls.Add(textBox1);
            myControls.Add(textBox2);
            myControls.Add(textBox3);
            //myControls.Add(textBox4);
            //myControls.Add(textBox5);
            myControls.Add(textBox6);
            //myControls.Add(textBox7);
            
            button1.Enabled = false;

            ShowErrors();
        }

        private void ShowErrors()
        {
            string test = "";

            test += SetErrorFormat(textBox1);
            test += SetErrorFormat(textBox2);
            test += SetErrorFormat(textBox3);
            label8.Text = "Listado de errores: \n";
            label8.Text += test;

            if (test != "")
            {
                label8.Visible = true;
            }
            else
            {
                label8.Visible = false;
            }
            ValidateButton(test);
        }

        private string SetErrorFormat(Control control)
        {
            string res;

            if (errorProvider1.GetError(control) != "")
            {
                res = string.Format("- {0} \n", errorProvider1.GetError(control));
            }
            else
                res = "";
            return res;

        }

        #region Validation
        private void ValidateButton(string test)
        {
            if (test == "" && myControls.Where(x => x.Text.Length == 0).Count() == 0)
            {
                button1.Enabled = true;
            }
            else
                button1.Enabled = false;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ShowErrors();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ShowErrors();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            ShowErrors();
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            ShowErrors();
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsSymbol(e.KeyChar) || char.IsWhiteSpace(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten números.");
                e.Handled = true;
                return;
            }
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsSymbol(e.KeyChar) || char.IsWhiteSpace(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten números.");
                e.Handled = true;
                return;
            }
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsSymbol(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten números.");
                e.Handled = true;
                return;
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            Person person = new Person();
            person.Name = textBox1.Text;
            person.Surname = textBox2.Text;
            person.cellphone1 = textBox3.Text;
            person.celphone2 = textBox4.Text;
            person.telephone = textBox5.Text;
            person.obra = textBox6.Text;
            person.email = textBox8.Text;
            person.extraInformaation = textBox7.Text;
            
            PersonService per = new PersonService();
            per.Add(person);
            MessageBox.Show("El contacto ha sido agendado con éxito.");
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
